<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>创建商品</title>

    <link rel="stylesheet" href="../static/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/global/css/components.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/admin/pages/css/login.css" type="text/css">
    <script src="../static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>

</head>

<body class="login">
<div class="content">
    <h3 class="form-title">创建商品</h3>

    <div class="form-group">
        <label class="control-label">商品名</label>
        <input class="form-control" type="text" name="title" id="title">
    </div>
    <div class="form-group">
        <label class="control-label">描述</label>
        <input class="form-control" type="text" name="description" id="description">
    </div>
    <div class="form-group">
        <label class="control-label">价格</label>
        <input class="form-control" type="text" name="price" id="price">
    </div>
    <div class="form-group">
        <label class="control-label">图片</label>
        <input class="form-control" type="text" name="imgUrl" id="imgUrl">
    </div>
    <div class="form-group">
        <label class="control-label">库存</label>
        <input class="form-control" type="text" name="stock" id="stock">
    </div>

    <div class="form-actions">
        <button class="btn blue" id="create" type="submit">
            提交
        </button>
    </div>
</div>


<script>
    $(function () {

        $("#create").click(function () {
            let title = $("#title").val();
            let description = $("#description").val();
            let price = $("#price").val();
            let imgUrl = $("#imgUrl").val();
            let stock = $("#stock").val();

            if (title === null || title === "") {
                alert("商品名不能为空");
                return false;
            }
            if (description === null || description === "") {
                alert("描述不能为空");
                return false;
            }
            if (price === null || price === "") {
                alert("价格不能为空");
                return false;
            }
            if (imgUrl === null || imgUrl === "") {
                alert("图片不能为空");
                return false;
            }
            if (stock === null || stock === "") {
                alert("库存不能为空");
                return false;
            }
            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                url: "http://localhost:8090/item/create",
                data: {
                    "title" : title,
                    "description" : description,
                    "price" : price,
                    "imgUrl" : imgUrl,
                    "stock" : stock,
                },
                xhrFields:{withCredentials : true},
                success: function (data) {
                    if (data.status === "success") {
                        alert("创建成功");
                    } else {
                        alert("创建失败，因为" + data.data.errMsg);
                    }
                },
                error: function (data) {
                    alert("创建失败，因为" + data.responseText);
                }
            })
        });
        return false;
    });
</script>

</body>
</html>